package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.MyFansAdapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.MyFansParser;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称：MyFansActivity   
 * 类描述：我的粉丝   
 * 创建人：耿卫斌  
 * 创建时间：2016年5月16日 下午2:28:05      
 *
 */
public class MyFansActivity extends BaseActivity implements OnClickListener {
	private MyFansAdapter adapter;
	private Context mContext;
	private PullableListView mListView;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private List<MyFansParser> list;
	private List<MyFansParser> more_list;
	private PullToRefreshLayout mPullLayout;
	private LinearLayout noDataLL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_fans);
		setContent(this, getIntent());
		mContext = this;
		mTittle = (TextView) findViewById(R.id.title_text);
		mTittle.setText("关注我的");
		setBack();
		mBack.setOnClickListener(this);
		list=new ArrayList<MyFansParser>();
		more_list=new ArrayList<MyFansParser>();
		noDataLL=(LinearLayout) findViewById(R.id.MyFans_noData);
		mListView =(PullableListView) findViewById(R.id.MyFans_listivew);
		mPullLayout = (PullToRefreshLayout)findViewById(R.id.MyFans_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getFansList();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				//上拉加载
				page++;
				getFansList();
			}
		});
		getFansList();
	}

	@Override
	public void onClick(View v) {

	}


	/**
	 * 获取通知列表
	 */
	private void getFansList(){
		VolleyBasic.getInstance(MyFansActivity.this).post(Cache.fanslist,
				VolleyMap.getInstance().fanslist(page,10), MyFansParser.class,new VolleyCallback() {
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list=(List<MyFansParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				if(more_list!=null && more_list.size()!=0){
					//这里是防止登陆的情况下增加关注数量
					AppUtil.getUser().setFollownumber(more_list.size()+"");
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
				if (adapter == null) {
					adapter=new MyFansAdapter(mContext, more_list);
					mListView.setAdapter(adapter);
				}
				adapter.notifyDataSetChanged();
				Log.d("log", more_list.toString());
			}
			public void onError(VolleyError volleyError) {
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
			}
		});
	}

}
